package org.iblink.gui.swing.actions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.AbstractAction;

public class CategoryAction extends AbstractAction {
  /**
   * Generated serial version UID
   */
  private static final long serialVersionUID = 7351848306394108835L;

  /**
   * Action key for adding a child category
   */
  public static final String ADDCHILD_ACTION = "ADDCHILD";
  /**
   * Action key for adding a sibling category
   */
  public static final String ADDSIBLING_ACTION = "ADDSIBLING";
  /**
   * Action key for renaming a category
   */
  public static final String RENAME_ACTION = "RENAMENODE";
  /**
   * Action key for deleting a category
   */
  public static final String DELETE_ACTION = "DELETENODE";
  
  private static CategoryAction renameCategoryAction;
  private static CategoryAction addChildCategoryAction;
  private static CategoryAction addSiblingCategoryAction;
  private static CategoryAction deleteCategoryAction;
  
  /**
   * Action listener
   */
  private List<ActionListener> listeners = new ArrayList<ActionListener>();
  
  /**
   * Action command key
   */
  private String command;
  
  /**
   * Category action constructor
   * 
   * @param text the text to be displayed in the context menu
   * @param listener the listener of this action
   * @param command the command key for this action
   */
  private CategoryAction(String text, String command) {
    super(text);
    
    this.command = command;
  }
  
  public void addActionListener(ActionListener listener) {
    if (!listeners.contains(listener)) {
      listeners.add(listener);
    }
  }
  
  public void removeActionListener(ActionListener listener) {
    if (listeners.contains(listener)) {
      listeners.remove(listener);
    }
  }

  @Override
  public void actionPerformed(ActionEvent event) {
    ActionEvent e = new ActionEvent(event.getSource(), event.getID(), command);
    for (Iterator<ActionListener> iter = listeners.iterator(); iter.hasNext();) {
      iter.next().actionPerformed(e);
    }
  }
  
  public static CategoryAction getRenameCategoryAction() {
    if (renameCategoryAction == null) {
      renameCategoryAction = new CategoryAction("Rename", CategoryAction.RENAME_ACTION);
      renameCategoryAction.setEnabled(false);
    }
    
    return renameCategoryAction;
  }
  
  public static CategoryAction getAddChildCategoryAction() {
    if (addChildCategoryAction == null) {
      addChildCategoryAction = new CategoryAction("Add child", CategoryAction.ADDCHILD_ACTION);
      addChildCategoryAction.setEnabled(false);
    }
    
    return addChildCategoryAction;
  }
  
  public static CategoryAction getAddSiblingCategoryAction() {
    if (addSiblingCategoryAction == null) {
      addSiblingCategoryAction = new CategoryAction("Add sibling", CategoryAction.ADDSIBLING_ACTION);
      addSiblingCategoryAction.setEnabled(false);
    }
    
    return addSiblingCategoryAction;
  }
  
  public static CategoryAction getDeleteCategoryAction() {
    if (deleteCategoryAction == null) {
      deleteCategoryAction = new CategoryAction("Delete", CategoryAction.DELETE_ACTION);
      deleteCategoryAction.setEnabled(false);
    }
    
    return deleteCategoryAction;
  }
}
